package com.xky.redis.controller;

import com.xky.redis.domain.User;
import com.xky.redis.service.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author keying.xia
 * @date 2017/11/30
 */
@RestController
public class RedisController {

    @Autowired
    public IRedisService redisService;

    @GetMapping(value = "/add")
    public void add(){
        User xky = new User();
        xky.setId(1);
        xky.setName("xky");
        xky.setPhone(1234314141L);
        xky.setAge(11);
        xky.setRedisKey("redis_key_xky");

        redisService.put(xky.getRedisKey(),xky,-1);

        User lpp = new User();
        lpp.setId(2);
        lpp.setName("lpp");
        lpp.setPhone(1111111L);
        lpp.setAge(12);
        lpp.setRedisKey("redis_key_lpp");
        redisService.put(lpp.getRedisKey(),lpp,-1);

    }

    @GetMapping(value = "/getAll")
    public Object getAll(){
        return redisService.getAll();
    }

    //查询所有key
    @GetMapping(value = "/getKeys")
    public Object getKeys() {
        return redisService.getKeys();
    }

    //根据key查询
    @GetMapping(value = "/get")
    public Object get() {
        User u = new User();
        u.setRedisKey("redis_key_xky");
        return redisService.get(u.getRedisKey());
    }

    //删除
    @GetMapping(value = "/remove")
    public void remove() {
        User u = new User();
        u.setRedisKey("redis_key_xky");
        redisService.remove(u.getRedisKey());
    }

    //判断key是否存在
    @GetMapping(value = "/isKeyExists")
    public void isKeyExists() {
        User m = new User();
        m.setRedisKey("redis_key_xky");
        boolean flag = redisService.isKeyExists(m.getRedisKey());
        System.out.println("redis_key_xky 是否存在: "+flag);
    }

    //查询当前缓存的数量
    @GetMapping(value = "/count")
    public Object count() {
        return redisService.count();
    }

    //清空所有key
    @GetMapping(value = "/empty")
    public void empty() {
        redisService.empty();
    }


}
